﻿using BarcodeDelay.bean;
using BarcodeDelay.utility;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarcodeDelay.business
{
    public class LeaveMessageHelperAuto
    {
        public bool isInLeaveProcess = false;
        private StreamWriter swb = null;
        FileStream bFile = null;
        ArrayList m_arListDepBarcodes = new ArrayList();
        SelectTimeBean selectTimeBean = new SelectTimeBean();
        public event Action AddMessage;
        public event Action QueryMessageRes;
        public event Action DoneHttpResponse;
        public event Action GotDelayBarcode;
        public event Action GotFailedBarcode;
        public event Action GotInterceptBarcode;

        public event Action DoneSBResponse;
        private static LeaveMessageHelperAuto instance;
        public static LeaveMessageHelperAuto Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LeaveMessageHelperAuto();
                }
                return instance;
            }
        }
        private Thread fThreadDep = null;
        public void BeginAnalyThread(SelectTimeBean selectTimeBean, string anayType)
        {
            try
            {
                StoParams.Instance.totalParcels = 0;
                StoParams.Instance.addCommentParcels = 0;
                StoParams.Instance.curDelayBarcode = "";
                StoParams.Instance.curFailedBarcode = "";
                StoParams.Instance.totalPages = 0;
                StoParams.Instance.curParces = 0;
                StoParams.Instance.isProcessStop = false;
                initLog();
                m_arListDepBarcodes.Clear();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    fThreadDep.Abort();
                    System.Threading.Thread.Sleep(50);
                }
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    while (fThreadDep.ThreadState != ThreadState.Aborted)
                    {
                        //当调用Abort方法后，如果thread线程的状态不为Aborted，主线程就一直在这里做循环，直到thread线程的状态变为Aborted为止
                        Thread.Sleep(100);
                    }
                }

                this.selectTimeBean = selectTimeBean;
                //m_arListDepBarcodes = arListBarCodes;
                fThreadDep = new Thread(new ThreadStart(handleAllBarcodes));//出港分析 开辟一个新的线程
                fThreadDep.IsBackground = true;
                fThreadDep.Start();

            }
            catch (Exception ex)
            {

            }
        }

        private void handleAllBarcodes()
        {
            try
            {
                StoParams.Instance.totalParcels = 0;
                isInLeaveProcess = true;

                string outmsg = "";
                ArrayList arListArbitrationBarcodes = new ArrayList();
                arListArbitrationBarcodes = JTDelayHelper.Instance.getArbitrationBarcodes(out outmsg);

                if (arListArbitrationBarcodes.Count > 0)
                {
                    StoParams.Instance.resMsg = "已申报数据下载成功，开始分析单号";
                }
                else
                {
                    StoParams.Instance.resMsg = "已申报数据下载失败";
                }

                DoneSBResponse.Invoke();
                System.Threading.Thread.Sleep(100);

                DateTime dtStart = this.selectTimeBean.startTime;
                DateTime dtEnd = this.selectTimeBean.endTime;
                JObject jObj = new JObject();
                jObj["current"] = 1;
                jObj["size"] = 100;
                jObj["startDate"] = dtStart.ToString("yyyy-MM-dd HH:mm:ss");
                jObj["endDate"] = dtEnd.ToString("yyyy-MM-dd HH:mm:ss"); ;
                jObj["scanSiteCode"] = StoParams.Instance.register_code;
                jObj["scanType"] = "收件";
                jObj["billType"] = 0;
                jObj["billNoList"] = new JArray();
                jObj["signType"] = "";
                jObj["countryId"] = "1";
                string postUrl = "https://jmsgw.jtexpress.com.cn/bigdataoperatingplatform/wayBillStatus/wayBillOrderStatusQuery";
                if (StoParams.Instance.isRabbitWin)
                {
                    postUrl = "https://wdgw.jtexpress.com.cn/ops/wdbutler/wayBillStatus/wayBillOrderStatusQuery";
                }

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string routeName = "waybillStatusConstantlyNew";
                string messge = HttpClient.HttpUtil.Instance.PostCommon(postUrl, postData, routeName);
                int totalPages = 0;
                ArrayList arListbarcodes = new ArrayList();
                if (messge.Contains("请求成功"))
                {
                    if (messge.Contains("msg"))
                    {
                        JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(messge);
                        JToken msgToken = jsonObjRes["msg"];
                        StoParams.Instance.resMsg = msgToken.ToString();
                        JToken dataObj = jsonObjRes["data"];
                        string totalCount = Convert.ToString(dataObj["total"].ToString());
                        string pageCount = Convert.ToString(dataObj["pages"].ToString());
                        StoParams.Instance.totalParcels = StoParams.Instance.totalParcels+ Convert.ToInt32(totalCount);
                        totalPages = Convert.ToInt32(pageCount);
                        Utility.parseBarcodes(arListbarcodes, (JObject)dataObj);

                    }
                    else
                    {
                        StoParams.Instance.resMsg = messge.ToString();
                    }
                    QueryMessageRes.Invoke();

                    
                    for(int curPage = 1;curPage<= totalPages; curPage++)
                    {
                        System.Threading.Thread.Sleep(6000);
                        while(StoParams.Instance.isProcessStop)
                        {
                            System.Threading.Thread.Sleep(500);
                        }
                        ArrayList arListAllBarcodes = new ArrayList();
                        getCurPageBarcodes(arListAllBarcodes, curPage);
              
                       

                        ArrayList arListDelayBarcodes = new ArrayList();
                        if(arListAllBarcodes.Count>0)
                        {
                            parseEachBarcode(arListAllBarcodes, arListDelayBarcodes, arListArbitrationBarcodes);
                        }
                        //break;

                    }
                }
                else
                {
                    StoParams.Instance.resMsg = "请求失败，请重新登录";
                    DoneHttpResponse();
                }
                isInLeaveProcess = false;


            }
            catch(Exception ex)
            {
                StoParams.Instance.resMsg = "请求失败，请重新登录";
                DoneHttpResponse();
            }
        }

        private bool addComment( string strBarCode,string commentType)
        {
            bool ret = false;
            try
            {
                string strInterCeptSiteCode = "";
                string siteName = "";
                string leaveMsg = "";
                string msg = "";
                ArrayList arListRoadMap = new ArrayList();
                ArrayList arListRoadGX = new ArrayList();
                arListRoadMap = JTDelayHelperNew.Instance.getRoadMap(strBarCode.Trim(), out msg, arListRoadGX);
                if (commentType=="拦截")
                {
                    leaveMsg = JTDelayHelper.Instance.getInterMsg(commentType,"");
                }
                else
                {
                    JTDelayHelper.Instance.parseManualBarcode(arListRoadMap, out commentType, out strInterCeptSiteCode, out siteName, out leaveMsg, false);
                }

                //string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/leaveMessage/getReceiveNetwork?waybillNo=" + strBarCode.Trim();
                JObject jObj = new JObject();
                JArray jarry = new JArray();
                jarry.Add(strBarCode);
                string strUrl = "";
                string res = "";
                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                //string res = HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "integratedQuery");


                SiteBean siteBean = UtilityHelp.Instance.getFirstSite(res, arListRoadMap);
                //SiteBean siteBean = UtilityHelp.Instance.getLastSite(res);
                if (siteBean.siteCode == "")
                {
                    siteBean = UtilityHelp.Instance.getBeanSite(arListRoadMap);
                }
                if (siteBean.siteCode == "" && siteBean.siteId == "")
                {
                    if (siteBean.siteName == StoParams.Instance.register_site)
                    {
                        
                    }
                    return ret;
                }
                if (siteBean.siteCode == "" && siteBean.siteId != "")
                {
                    try
                    {
                        strUrl = "https://jmsgw.jtexpress.com.cn/operatingplatform/networkInfo/getNetworkDetailInfoById?networkId=" + siteBean.siteId.Trim();
                        if (StoParams.Instance.isRabbitWin)
                        {
                            strUrl = "https://wdgw.jtexpress.com.cn/ops/wdbutler/networkInfo/getNetworkDetailInfoById?networkId=" + siteBean.siteId.Trim();
                        }
                        jObj = new JObject();
                        jarry = new JArray();
                        jarry.Add(strBarCode);

                        postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                        res = HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "trackingExpress");
                        if (res.Contains("请求成功"))
                        {
                           
                            if (res.Contains("data"))
                            {
                                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(res);

                                JObject jsonObjData = (JObject)jsonObjRes["data"]; ;
                                JToken networkCode = jsonObjData["networkCode"];
                                siteBean.siteCode = networkCode.ToString();

                            }

                        }
                    }
                    catch (Exception ex)
                    {

                    }

                }

                if (StoParams.Instance.register_site.Contains(siteBean.siteName.Trim())
                    || siteBean.siteName.Trim().Contains(StoParams.Instance.register_site))
                {
                    AddMessage.Invoke();
                    return ret;
                }
                jObj["waybillNo"] = strBarCode.Trim();
                jObj["receiveNetworkId"] = siteBean.siteId.Trim();
                jObj["fileUrl"] = "";
                jObj["receiveNetworkName"] = siteBean.siteName.Trim();
                jObj["receiveNetworkCode"] = siteBean.siteCode.Trim();
                jObj["countryId"] = "1";
                jObj["leaveMessageType"] = 2;

                string wechatName = StoParams.Instance.register_wechat;
                string tmplete = leaveMsg + " "  + "协商加微信 " + wechatName;      
                
                jObj["replyContent"] = tmplete;
                //string postUrl = "https://jmsgw.jtexpress.com.cn/servicequality/leaveMessage/add";
                string postUrl = "https://jmsgw.jtexpress.com.cn/operatingplatform/messageBoard/add";
                if (StoParams.Instance.isRabbitWin)
                {
                    postUrl = "https://wdgw.jtexpress.com.cn/ops/wdbutler/messageBoard/add";
                }
                postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);

                //return;
                string messge = HttpClient.HttpUtil.Instance.addLeaveMessage(postUrl, postData);
                if (messge.Contains("成功"))
                {
                    logHelp.LogHelper.Instance.writeLog(strBarCode + "--" + tmplete);
                    ret = true;
                    StoParams.Instance.resMsg = "留言成功";
                }
                else
                {
                    StoParams.Instance.resMsg = "留言失败";
                }
                QueryMessageRes.Invoke();
            }
            catch(Exception ex)
            {

            }
            return ret;
        }

        private void parseEachBarcode(ArrayList arListBarcodes,ArrayList arListDelayBarcodes,ArrayList arListArbitrationBarcodes)
        {
            try
            {
                int sumQueryTime = 0;
                int avgQueryTime = 2;
                int intProcess = 0;
                string filter = "";

                intProcess++;
                //StoParams.Instance.curParces = StoParams.Instance.curParces + (100 - arListBarcodes.Count);
                ArrayList arListDepResultTmp = new ArrayList();
                string httpRes = "";
                foreach (string strBarCode in arListBarcodes)
                {
                    StoParams.Instance.curParces = StoParams.Instance.curParces + 1;
                    while (StoParams.Instance.isProcessStop)
                    {
                        System.Threading.Thread.Sleep(200);
                    }

                    
                    string outMsg = "";
                    bool isReturnInRepley = false;
                    string lastUpdateTime = "";
      
                    JTDelayHelper.Instance.getCommentsContent(strBarCode, out outMsg, out lastUpdateTime);
                    if (arListArbitrationBarcodes.Contains(strBarCode)|| outMsg.Contains("已理赔"))
                    {
                        //StoParams.Instance.curParces++;
                        continue;
                    }
                    if(lastUpdateTime!="")
                    {
                        TimeSpan tsCur = System.DateTime.Now - Convert.ToDateTime(lastUpdateTime);
                        if(tsCur.TotalHours<24)
                        {
                            //StoParams.Instance.curParces++;
                            continue;
                        }
                    }
                   
                    if (outMsg.Contains("退回"))
                    {
                        isReturnInRepley = true;
                    }
                    ResultBean resultBean = new ResultBean();
                    StoParams.Instance.currentBarcode = strBarCode;
                    StoParams.Instance.resMsg = "";
                    string strResponse = "";

                    ArrayList arListAllGXTask = new ArrayList();
                    ArrayList arListRoadMap =JTDelayHelper.Instance.getRoadMap(strBarCode, out httpRes, arListAllGXTask);
                    //ArrayList arListRoadMap = getRoadMap("JT5105783002188", out httpRes, arListAllGXTask);
                    string assignTme = "";
                    string dispatchSite = "";
                    string dispatchSiteArrival = "";
                    string terminalDepTime = "";
                    ArrayList arListDBOrJB = new ArrayList();
                    StoParams.Instance.curParces++;
                    if (!httpRes.Contains("成功"))
                    {
                        StoParams.Instance.resMsg = httpRes;
                        DoneHttpResponse();
                        System.Threading.Thread.Sleep(500);
                        continue;
                    }
                    string commentType = "";
                    string strInterCeptSiteCode = "";
                    string pickName = "";
                    string orderSource = "";

                    string curSiteToken = "";
                    string customerName = "";
                    if (!UtilityHelp.Instance.resetToken(strBarCode, out pickName, out orderSource, out curSiteToken,out customerName))
                    {

                        if (curSiteToken == "")
                        {
                            StoParams.Instance.htResWechat[strBarCode] = " 发件网点 " + pickName + " 登录失效";
                            continue;
                        }
                        StoParams.Instance.htResWechat[strBarCode] = " 发件网点 " + pickName + " 非我司单号  无法处理";
                        continue;

                    }
                    if (pickName.Contains("授权"))
                    {
                        continue;
                    }
                    

                    if (JTDelayHelper.Instance.isNoSiteToLeaveMsg(arListRoadMap, pickName))
                    {
                        System.Threading.Thread.Sleep(300);
                        StoParams.Instance.interResMsg = "快件未到转运";
                        continue;
                    }


                    if (JTDelayHelper.Instance.isInterceptDelay(arListRoadMap))
                    {
                        StoParams.Instance.interBarcode = strBarCode;
                        GotInterceptBarcode();
                        commentType = "拦截";
                        bool resAddCommnetIntercept = addComment(strBarCode, commentType);
                        if (resAddCommnetIntercept)
                        {
                            StoParams.Instance.interBarcode = strBarCode;
                        }
                        else
                        {
                            if (StoParams.Instance.interResMsg == "")
                            {
                                StoParams.Instance.interResMsg = "留言失败";
                            }

                        }
                        StoParams.Instance.interResMsg = strBarCode + "\r\n" + "超时未退回" + "\r\n" + StoParams.Instance.interResMsg;
                        DoneHttpResponse();
                        Thread.Sleep(300);
                        continue;
                    }

                    

                    bool isDelay = JTDelayHelper.Instance.parseAutoBarcode(arListRoadMap,out commentType,out strInterCeptSiteCode);
                    if(isDelay)
                    {
                        arListDelayBarcodes.Add(strBarCode);                        
                        
                        //if(UtilityHelp.Instance.addCommentNew(strBarCode, commentType))
                        if(addComment(strBarCode, commentType))
                        {
                            StoParams.Instance.addCommentParcels++;
                            StoParams.Instance.curDelayBarcode = strBarCode;
                            StoParams.Instance.resMsg = "留言成功";
                            GotDelayBarcode();
                        }
                        else
                        {
                            StoParams.Instance.curFailedBarcode = strBarCode;
                            StoParams.Instance.resMsg = "留言失败";
                            GotFailedBarcode();
                        }
                                                
                    }                    


                    if (!httpRes.Contains("成功"))
                    {
                        StoParams.Instance.resMsg = httpRes;
                    }
                    
                    else
                    {
                        if (!isDelay)
                        {
                            StoParams.Instance.resMsg = "无延误";
                        }
                            
                    }
                    
                    DoneHttpResponse();
                    Random rd = new Random();
                    int sleepTime = rd.Next(500, 1000);
                    System.Threading.Thread.Sleep(sleepTime);
                }
                
                Program.m_frmain.SetTextMessage(intProcess, m_arListDepBarcodes.Count, "");
            }
            catch(Exception ex)
            {

            }
        }


        

        private void getCurPageBarcodes(ArrayList arListBarcodes,int curPage)
        {
            try
            {
                DateTime dtStart = this.selectTimeBean.startTime;
                DateTime dtEnd = this.selectTimeBean.endTime;
                JObject jObj = new JObject();
                jObj["current"] = curPage;
                jObj["size"] = 100;
                jObj["startDate"] = dtStart.ToString("yyyy-MM-dd HH:mm:ss");
                jObj["endDate"] = dtEnd.ToString("yyyy-MM-dd HH:mm:ss"); ;
                jObj["scanSiteCode"] = StoParams.Instance.register_code;
                jObj["scanType"] = "收件";
                jObj["billType"] = 0;
                jObj["billNoList"] = new JArray();
                jObj["signType"] = "";
                jObj["countryId"] = "1";
                string postUrl = "https://jmsgw.jtexpress.com.cn/bigdataoperatingplatform/wayBillStatus/wayBillOrderStatusQuery";
                if (StoParams.Instance.isRabbitWin)
                {
                    postUrl = "https://wdgw.jtexpress.com.cn/ops/wdbutler/wayBillStatus/wayBillOrderStatusQuery";
                }
                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string routeName = "waybillStatusConstantlyNew";
                string messge = HttpClient.HttpUtil.Instance.PostCommon(postUrl, postData, routeName);
                int totalPages = 0;
                ArrayList arListbarcodes = new ArrayList();
                if (messge.Contains("请求成功"))
                {
                    if (messge.Contains("msg"))
                    {
                        JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(messge);
                        JToken msgToken = jsonObjRes["msg"];
                        StoParams.Instance.resMsg = msgToken.ToString();
                        JToken dataObj = jsonObjRes["data"];
                        string totalCount = Convert.ToString(dataObj["total"].ToString());
                        string pageCount = Convert.ToString(dataObj["pages"].ToString());
                        
                        StoParams.Instance.totalParcels = Convert.ToInt32(totalCount);
                        totalPages = Convert.ToInt32(pageCount);
                        Utility.parseBarcodes(arListBarcodes, (JObject)dataObj);

                    }
                    else
                    {
                        StoParams.Instance.resMsg = messge.ToString();
                    }
                    QueryMessageRes.Invoke();
                }

            }
            catch (Exception ex)
            {

            }
        }

        
        

        private void parseDepartureBarcodes()
        {
            int totalDelays = 0;
            int queryServerResTime = 0;
            int priorBarCodeCount = 0;
            ArrayList arListDepResultTmp = new ArrayList();
            try
            {
                int totalQueryCount = 0;
                ArrayList arListBarcodes = new ArrayList();
                ArrayList arListTmp = new ArrayList();
                RoadInfo roadInfoGen = new bean.RoadInfo();
                Hashtable htRoadMap = new Hashtable();
                bool isNewStart = false;
                string barcode = "";
                ArrayList arListBarCodes = new ArrayList();
                arListBarCodes.Clear();
                DateTime dtStart = System.DateTime.Now;
                DelayAnaHelperGen.Instance.m_arlist_Delayed_BarCodes.Clear();


                if (arListTmp.Count > 0)
                {
                    arListBarcodes.Add(arListTmp.Clone());
                    arListTmp.Clear();
                }
                int sumQueryTime = 0;
                int avgQueryTime = 2;
                int intProcess = 0;

                foreach (ArrayList arListBarcode in m_arListDepBarcodes)
                {
                    intProcess++;

                    foreach (string strBarCode in arListBarcode)
                    {
                        string strLeftTime = "10";

                        /*StoParams.Instance.currentBarcode = strBarCode.Trim();
                        QueryMessageRes.Invoke();
                        Random ran2 = new Random();


                        int sleepTime2 = ran2.Next(3000, 5000);
                        if (StoParams.Instance.isFastMode)
                        {
                            sleepTime2 = ran2.Next(2000, 3000);
                        }

                        Thread.Sleep(sleepTime2);
                        Program.m_frmain.SetTextMessage(intProcess, m_arListDepBarcodes.Count, "00:00:02");
                        AddMessage.Invoke();
                        StoParams.Instance.currentBarcode = strBarCode.Trim();
                        continue;*/



                        //string strUrl = "https://jmsgw.jtexpress.com.cn/operatingplatform/historyPodTracking/inner/query/keywordList";
                        string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/leaveMessage/getReceiveNetwork?waybillNo="+strBarCode.Trim();
                        JObject jObj = new JObject();                     
                        JArray jarry = new JArray();
                        totalQueryCount = totalQueryCount + 1;
                        jarry.Add(strBarCode);
            
                        string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);

                        //string postData = "{\"isAllPod\":1,\"keywordList\":[\"JT5036358713908\",\"JT5036397106300\"],\"trackingTypeEnum\":\"WAYBILL\",\"countryId\":\"1\"}";
                        string res = HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData,"GET", "integratedQuery");
                        //DateTime dtNow = System.DateTime.Now;
                        /*if(!res.Contains("请求成功"))
                        {
                            if(res.Contains("msg"))
                            {
                                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(res);
                                JToken msgToken = jsonObjRes["msg"];
                                StoParams.Instance.resMsg = msgToken.ToString();
                            }
                            else
                            {
                                StoParams.Instance.resMsg = res.ToString();
                            }
                            QueryMessageRes.Invoke();
                            continue;
                        }*/
                        string msg = "";
                        ArrayList arListRoadMap = new ArrayList();
                        ArrayList arListRoadGX = new ArrayList();
                        arListRoadMap=JTDelayHelperNew.Instance.getRoadMap(strBarCode.Trim(), out msg, arListRoadGX);
                        SiteBean siteBean = UtilityHelp.Instance.getFirstSite(res, arListRoadMap);
                        //SiteBean siteBean = UtilityHelp.Instance.getLastSite(res);
                        
                        if (siteBean.siteCode=="")
                        {
                            continue;
                        }
                        if(MemberInfo.Instance.filter.Contains(siteBean.siteName)|| StoParams.Instance.register_site.Contains(siteBean.siteName.Trim())
                            || siteBean.siteName.Trim().Contains(StoParams.Instance.register_site))
                        {
                            AddMessage.Invoke();
                            continue;
                        }
                        jObj["waybillNo"] = strBarCode.Trim();
                        jObj["receiveNetworkId"] = siteBean.siteId.Trim();
                        jObj["fileUrl"] = "";
                        jObj["receiveNetworkName"] = siteBean.siteName.Trim();
                        jObj["receiveNetworkCode"] = siteBean.siteCode.Trim();
                        jObj["countryId"] = "1";
                        jObj["replyContent"] = MemberInfo.Instance.templeteTransfer;
                        jObj["leaveMessageType"] = 3;
                        //string postUrl = "https://jmsgw.jtexpress.com.cn/servicequality/leaveMessage/add";
                        string postUrl = "https://jmsgw.jtexpress.com.cn/operatingplatform/messageBoard/add";
                        postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);

                        //return;
                        string messge=HttpClient.HttpUtil.Instance.addLeaveMessage(postUrl, postData);
                        if(messge.Contains("成功"))
                        {
                            StoParams.Instance.resMsg = "留言成功";
                        }
                        else
                        {
                            StoParams.Instance.resMsg = "留言失败";
                        }
                        QueryMessageRes.Invoke();
                        Random ran = new Random();

                        
                        int sleepTime = ran.Next(3000, 5000);
                        if(StoParams.Instance.isFastMode)
                        {
                            sleepTime = ran.Next(1000, 2000);
                        }
                        Thread.Sleep(sleepTime);
                        StoParams.Instance.currentBarcode = strBarCode.Trim();
                        TimeSpan tsp = System.DateTime.Now - dtStart;
                        string spent = tsp.ToString(@"hh\:mm\:ss");
                        Program.m_frmain.SetTextMessage(intProcess, m_arListDepBarcodes.Count, spent);
                        AddMessage.Invoke();
                        System.DateTime dtBegin = System.DateTime.Now;

                        if (res.Length > 300)
                        {
                            queryServerResTime++;
                        }
                        else
                        {
                            continue;
                        }
                        lock (Program.obj_left_count)
                        {
                            StoParams.Instance.day_left = StoParams.Instance.day_left - arListBarcode.Count;
                            if (StoParams.Instance.day_left <= 0)
                            {
                                StoParams.Instance.day_left = 0;
                                //break;//暂时去掉
                            }
                        }
                        //continue;//暂时不解析
                        System.DateTime dtEndT = System.DateTime.Now;
                        TimeSpan tsSpend = dtEndT - dtBegin;
                        int oneQuery = Convert.ToInt32(tsSpend.TotalSeconds);
                        sumQueryTime = sumQueryTime + oneQuery;
                        avgQueryTime = Convert.ToInt32(sumQueryTime / intProcess);
             
                    }




                }



            }
            catch (Exception ex)
            {

            }
            int tiot = queryServerResTime;
        }

        public void stopThread()
        {
            try
            {
                ArrayList arListT = new ArrayList();
                ResultBean resultBean = new ResultBean();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    /*if (totalQueryCount > 0)
                    {
                        resultBean.countTime = totalQueryCount;

                        updateQueryCount(resultBean);
                    }*/
                    fThreadDep.Abort();
                    if (swb != null)
                    {
                        swb.Close();
                    }

                }

    
            }
            catch (Exception ex)
            {

            }
        }


        private void initLog()
        {
            try
            {
                string dir = System.Environment.CurrentDirectory;
                bFile = new FileStream(dir + "\\error.txt", FileMode.Create);
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                    swb.WriteLine("begin");

                }
            }
            catch (Exception ex)
            {

            }

        }


    }
}
